package hns;


import java.util.HashMap;

import org.jibble.pircbot.PircBot;


/**
 * This is the main bot which outsources events in the IRC channel to
 * the service modules.
 * 
 * DO NOT TAMPER WITH, UNLESS YOU KNOW WHAT YOU ARE DOING.
 * @author c03
 *
 */
public class Bot extends PircBot {
	
	private static Bot bot = null;
	private String botName;
	private HashMap<String,Service> servicesMap;
	

	public HashMap<String, Service> getServicesMap() {
		return servicesMap;
	}

	private Bot(){
		this.botName = "[NoFun]Abot";
		init();
	}
		
	public static Bot getInstance(){
		if(bot == null)
			bot = new Bot();
		return bot;
	}

	private void init() {
		setName(botName);
		servicesMap = new HashMap<String,Service>();
	}
	
	@Override
	protected void onJoin(String channel, String sender, String login,
			String hostname) {
		
		// Loop through commands, call onJoin
		for(Service s : servicesMap.values()){
			s.onJoin(channel, sender, login, hostname);
		}
		
	}
	
	@Override
	protected void onMessage(String channel, String sender, String login,
			String hostname, String message) {
		
		// Loop through commands, call appropriate implemented service
		for(String command : servicesMap.keySet()){
			if(command != null)
				if(message.startsWith(command))
					servicesMap.get(command).onMessage(channel, sender, login, hostname, message);
		}
		
		
	}
	
	@Override
	protected void onPrivateMessage(String sender, String login,
			String hostname, String message) {
		
		onMessage(sender, sender, login, hostname, message);
	}
	
	/**
	 * Add a service to the IRC Bot. Must be a Service (interface) implementation
	 * @param message The message that the bot will react to (ex. !google)
	 * @param service 
	 * @return
	 */
	public boolean addService(String command, Service service){
		if(!servicesMap.containsKey(command)){
			servicesMap.put(command,service);
			return true;
		}
			
		return false;
	}

}
